/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MVC;

import ChatNI.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//import common.*;

/**
 * Onglet
 * Class specific for creating the tab of conversation
 * @author Duy Duc, Metangmo
 */
public class Onglet extends JPanel implements  KeyListener{
    
    private JTextArea aireEmission = new JTextArea (5,30);
    private JTextArea aireReception = new JTextArea (20,28);
    private ChatController controller;
    private InetAddress destination ;
    
    /**
     * Constructor
     * @param controller
     *        Instance of the ChatController
     * @param address
     *        IP Address of the user for whom we want to open a tab of conversation
     */

    //Each tab is linked to an IP adrresse since its creation
    public Onglet(ChatController controller , InetAddress address) {
        
        this.controller=controller;
        this.destination = address;
        
         Font font = new Font("Verdana", Font.BOLD, 14);
        
         aireEmission.setBackground(Color.white);
	 aireReception.setBackground(Color.white);
         
         // Font of the emission and reception areas
         aireEmission.setFont(font);
         aireEmission.setForeground(Color.BLUE);
         aireReception.setFont(font);
         aireReception.setForeground(Color.BLUE);
         
         
          //The JTextArea is put in the JScrollPane in order to have a scroll bar
	  JScrollPane scroll = new JScrollPane(aireReception);
	  scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	  aireReception.setEditable(false);
          
          JScrollPane scroll2 = new JScrollPane(aireEmission);
	  scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	  aireEmission.setEditable(true);
	      	      
          aireEmission.setLineWrap(true);
          aireReception.setLineWrap(true);
          //aireEmission.setPreferredSize(new Dimension(10,30));
          //aireEmission.setMaximumSize(new Dimension(10,30));
          //aireEmission.setMinimumSize(new Dimension(10,30));
          
          
          aireEmission.addKeyListener(this);
          this.add(scroll);
          this.add(scroll2);

         
    }

    @Override
    public void keyTyped(KeyEvent ke) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    /**
     * keyPressed:
     * function verifies if the ENTER button is pressed for sending a message
     * @param ke
     *        KeyEvent
     */
    @Override
    public void keyReleased(KeyEvent ke) {
        int code = ke.getKeyCode ();
        if (code==KeyEvent.VK_ENTER){
            try {
                System.out.println(" je viens de cliquer sur entrée");
                aireReception.append(" [MOI] : "+aireEmission.getText()+"\n");
                this.sendMessage();
            } catch (UnknownHostException ex) {
                Logger.getLogger(Onglet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    
    
    @Override
    public void keyPressed(KeyEvent ke) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    /**
     * sendMessage:
     * function in charge of sending a TEXT message to a chosen user or a chosen group of user
     * @throws UnknownHostException 
     */
    public void sendMessage() throws UnknownHostException {
        
         controller.performSendMessage(FabriqueMessage.TypeMessage.TEXT,this.destination, null,null,aireEmission.getText());
         aireEmission.setText(null);
    }
    
    /**
     * getAireReception:
     * function collects the received text from a distance user
     * @return
     *        Content of the received text
     */
     
    public JTextArea getAireReception() {
        return aireReception;
    }
    
    /**
     * getAireEmission:
     * function collects the sent text to a distance user
     * @return 
     *        Content of the sent text
     */
    public JTextArea getAireEmission() {
        return aireEmission;
    }
    

    
}